﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using NUnit.Framework;

namespace Comdiv.UXmlDiff.Test {
	public class XDiffTestBase {
		protected void xmlidtest(string xml, string xpath, string expectedId = "", bool testXpathReverse = true, bool safemode = false) {
			var x = XElement.Parse(xml);
			XObject node = null;
			var doc = new XDocument();
			doc.Add(x);
			var result = doc.XPathEvaluate(xpath) as IEnumerable<object>;
			node = (XObject) result.FirstOrDefault();
			var id = new XDiffIdGenerator().GenerateId(node,safemode);

			//test that id was generate validely (only if test given)
			if (!string.IsNullOrWhiteSpace(expectedId)) {	
				Assert.AreEqual(expectedId, id);
			}
			
			//test that id can process xml back
			if (testXpathReverse) {
				var testresult = doc.XPathEvaluate(id) as IEnumerable<object>;
				Assert.AreEqual(1, testresult.Count());
				Assert.AreEqual(node, testresult.First());
			}

		}
	}
}